#include <stdio.h>
#include <string.h>

int add(int, int);
int change(int *,int *);

/* 定义简单的结构 */
struct
{
    unsigned int widthValidated;
    unsigned int heightValidated;
} status1;

/* 定义位域结构 */
struct
{
    unsigned int widthValidated : 1;
    unsigned int heightValidated : 1;
} status2;

struct {
    unsigned int age:3;
} Age;

struct Books
{
    char  title[50];
    char  author[50];
    char  subject[100];
    int   book_id;
};
struct bs{
    int a:8;
    int b:2;
    int c:6;
} bookShow;
struct bsPoint{
    unsigned a:1;
    unsigned b:3;
    unsigned c:4;
} bit,*pbit;

/* 函数声明 */
void showBook( struct Books book );
void printBook(struct Books *books);

int main() {
    int a=3,b=4;
    Age.age=a;
    printf("Hello, World!\n");
    printf("%s%d---->%d,%d\n","After Change:", change(&a,&b),a,b);
    printf("%s%d,%d\n","After:",a,b);
    printf("%s%d\n","The result is:", add(a,b));

    printf( "Memory size occupied by status1 : %d\n", sizeof(status1));
    printf( "Memory size occupied by status2 : %d\n", sizeof(status2));

    printf("Memory size occupied by Age:%d\n",sizeof(Age));
    printf("The age is %d\n",Age.age);

    printf("%s\n","====================================================================");
    struct Books Book1;        /* 声明 Book1，类型为 Book */
    struct Books Book2;        /* 声明 Book2，类型为 Book */

    /* Book1 详述 */
    strcpy( Book1.title, "C Programming");
    strcpy( Book1.author, "Nuha Ali");
    strcpy( Book1.subject, "C Programming Tutorial");
    Book1.book_id = 6495407;

    /* Book2 详述 */
    strcpy( Book2.title, "Telecom Billing");
    strcpy( Book2.author, "Zara Ali");
    strcpy( Book2.subject, "Telecom Billing Tutorial");
    Book2.book_id = 6495700;

    /* 输出 Book1 信息 */
    printf( "Book 1 title : %s\n", Book1.title);
    printf( "Book 1 author : %s\n", Book1.author);
    printf( "Book 1 subject : %s\n", Book1.subject);
    printf( "Book 1 book_id : %d\n", Book1.book_id);

    /* 输出 Book2 信息 */
    printf( "Book 2 title : %s\n", Book2.title);
    printf( "Book 2 author : %s\n", Book2.author);
    printf( "Book 2 subject : %s\n", Book2.subject);
    printf( "Book 2 book_id : %d\n", Book2.book_id);

    printf("============================================\n");

    /* 输出 Book1 信息 */
    showBook( Book1 );

    /* 输出 Book2 信息 */
    showBook( Book2 );
    printf("================结构指针测试=====================\n");
   // printBook(&Book1);
   // printBook(&Book2);
    printf("===================================\n");
    bookShow.a=3;
    printf("%d\n",bookShow.a);
    printf("=========================================================\n");
    bit.a=1; /* 给位域赋值（应注意赋值不能超过该位域的允许范围） */
    bit.b=7;    /* 给位域赋值（应注意赋值不能超过该位域的允许范围） */
    bit.c=15;   /* 给位域赋值（应注意赋值不能超过该位域的允许范围） */
    printf("%d,%d,%d\n",bit.a,bit.b,bit.c);  /* 以整型量格式输出三个域的内容 */
    pbit=&bit;    /* 把位域变量 bit 的地址送给指针变量 pbit */
    pbit->a=0;   /* 用指针方式给位域 a 重新赋值，赋为 0 */
    pbit->b&=3;  /* 使用了复合的位运算符 "&="，相当于：pbit->b=pbit->b&3，位域 b 中原有值为 7，与 3 作按位与运算的结果为 3（111&011=011，十进制值为 3） */
    pbit->c|=1;  /* 使用了复合位运算符"|="，相当于：pbit->c=pbit->c|1，其结果为 15 */
    printf("%d,%d,%d\n",pbit->a,pbit->b,pbit->c);    /* 用指针方式输出了这三个域的值 */
    return 0;
}

int add(int a, int b) {
    int result = 0;
    result = a + b;
    return result;
}
int change(int *pInt1,int *pInt2){
    *pInt1+=2;
    *pInt2+=3;
    return 0;
}
void showBook( struct Books book )
{
    printf( "Book title : %s\n", book.title);
    printf( "Book author : %s\n", book.author);
    printf( "Book subject : %s\n", book.subject);
    printf( "Book book_id : %d\n", book.book_id);
}
void printBook(struct Books *books){
    printf("Book title is:%s\n",books->title);
    printf("Book author is:%s\n",books->author);
    printf("Book subject is:%s\n",books->subject);
    printf("Book book_id is:%s\n",books->book_id);
}
